﻿using GKQC.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Machine
{
    public class SpeechRecognition
    {
        public SpeechRecognitionEngine recognizer = null;//语音识别引擎
        public DictationGrammar dictationGrammar = null; //自然语法
        public System.Windows.Forms.Control cDisplay; //显示控件

        public SpeechRecognition(string[] fg) //创建关键词语列表
        {
            CultureInfo myCIintl = new CultureInfo("zh-CN");
            foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//获取所有语音引擎
            {
                //if (config.Culture.Equals(myCIintl) && config.Id == "MS-1033-80-DESK")
                if (config.Culture.Equals(myCIintl))
                {
                    recognizer = new SpeechRecognitionEngine(config);
                    break;
                }//选择识别引擎
            }
            if (recognizer != null)
            {
                InitializeSpeechRecognitionEngine(fg);//初始化语音识别引擎
                dictationGrammar = new DictationGrammar();
            }
            else
            {
                MessageBox.Show("创建语音识别失败5");
            }
        }
        private void InitializeSpeechRecognitionEngine(string[] fg)
        {
            recognizer.SetInputToDefaultAudioDevice();//选择默认的音频输入设备
            Grammar customGrammar = CreateCustomGrammar(fg);
            //根据关键字数组建立语法
            recognizer.UnloadAllGrammars();
            recognizer.LoadGrammar(customGrammar);
            //加载语法
            recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
            recognizer.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);
        }



        public void BeginRec(Control tbResult)//关联窗口控件
        {
            TurnSpeechRecognitionOn();
            TurnDictationOn();
            cDisplay = tbResult;
        }
        public void over()//停止语音识别引擎
        {
            TurnSpeechRecognitionOff();
        }
        public virtual Grammar CreateCustomGrammar(string[] fg) //创造自定义语法
        {
            GrammarBuilder grammarBuilder = new GrammarBuilder();
            grammarBuilder.Append(new Choices(fg));
            return new Grammar(grammarBuilder);
        }
        private void TurnSpeechRecognitionOn()//启动语音识别函数
        {
            if (recognizer != null)
            {
                recognizer.RecognizeAsync(RecognizeMode.Multiple); //识别模式为连续识别
            }
            else
            {
                Logger.LogMessage("创建语音识别失败4");
            }
        }
        private void TurnSpeechRecognitionOff()//关闭语音识别函数
        {
            if (recognizer != null)
            {
                recognizer.RecognizeAsyncStop();
                TurnDictationOff();
            }
            else
            {
                Logger.LogMessage("创建语音识别失败3");
            }
        }
        private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            //识别出结果完成的动作，通常把识别结果传给某一个控件
            string text = e.Result.Text;
            cDisplay.Text = text;
        }

        private void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
        {
            string text = e.Result.Text;
            cDisplay.Text = text;
        }
        private void TurnDictationOn()
        {
            if (recognizer != null)
            {
                recognizer.LoadGrammar(dictationGrammar);
                //加载自然语法
            }
            else
            {
                Logger.LogMessage("创建语音识别失败1");
            }
        }
        private void TurnDictationOff()
        {
            if (dictationGrammar != null)
            {
                recognizer.UnloadGrammar(dictationGrammar);
                //卸载自然语法
            }
            else
            {
                Logger.LogMessage("创建语音识别失败2");
            }
        }

        void demo()
        {
            using (SpeechRecognizer recognizer =
         new SpeechRecognizer())
            {

                // Create a grammar.  
                //  Create lists of alternative choices.  
                Choices listTypes = new Choices(new string[] { "albums", "artists", "你好" });
                Choices genres = new Choices(new string[] {
          "blues", "classical", "gospel", "jazz", "rock", "你好" });

                //  Create a GrammarBuilder object and assemble the grammar components.  
                GrammarBuilder mediaMenu = new GrammarBuilder("Display the list of");
                mediaMenu.Append(listTypes);
                mediaMenu.Append("in the");
                mediaMenu.Append(genres);
                mediaMenu.Append("category.");

                //  Build a Grammar object from the GrammarBuilder.  
                Grammar mediaMenuGrammar = new Grammar(mediaMenu);
                mediaMenuGrammar.Name = "Media Chooser";

                // Attach event handlers.  
                recognizer.LoadGrammarCompleted +=
                  new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
                recognizer.SpeechRecognized +=
                  new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized1);
                recognizer.SpeechHypothesized +=
                  new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized1);

                // Load the grammar object to the recognizer.  
                recognizer.LoadGrammarAsync(mediaMenuGrammar);

                // Keep the console window open.  
                Console.ReadLine();
            }
        }

        // Handle the SpeechHypothesized event.  
        void recognizer_SpeechHypothesized1(object sender, SpeechHypothesizedEventArgs e)
        {
            //label1.Text = e.Result.Text;
        }

        // Handle the LoadGrammarCompleted event.  
        void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
        {
            //label1.Text = e.Grammar.Name;
        }

        // Handle the SpeechRecognized event.  
        void recognizer_SpeechRecognized1(object sender, SpeechRecognizedEventArgs e)
        {
            //label1.Text = e.Result.Text;
        }
    }
}
